package com.example.mvpdemo.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseMVPFragment<T extends BasePresenter> extends BaseFragment {

    /***
     * 泛型Presenter
     */
    protected T mPresenter;

    /***
     * 初始化presenter
     */
    protected abstract void initPresenter();

    /***
     * 填充布局
     */
    protected abstract int getLayout();

    /***
     * 初始化view
     */
    protected abstract void initView(View view);

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayout(), container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initPresenter();
        initView(view);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null && mPresenter.isViewAttached()) {
            mPresenter.detachView();
            mPresenter = null;
        }

    }
}
